package com.example.app.ui.Discover;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.app.PostActivity;
import com.example.app.R;
import com.example.app.databinding.FragmentDiscoverBinding;
import java.util.ArrayList;
import java.util.List;
public class DiscoverFragment extends Fragment {
    private FragmentDiscoverBinding binding;
    private DiscoverAdapter adapter;
    private List<Post> postList = new ArrayList<>();
    private void addStatic() {
        Post post1 = new Post();
        post1.avatarRes = R.drawable.xm;
        post1.author = "小美";
        post1.content = "美好的周末！";
        post1.imageRes = R.drawable.tx;
        post1.time = "刚刚";
        post1.comments.add(new Comment("小明", "拍得真好看！"));
        post1.comments.add(new Comment("小红", "周末愉快～"));
        postList.add(post1);
        Post post2 = new Post();
        post2.avatarRes = R.drawable.tx;
        post2.author = "科技达人";
        post2.content = "新入手的旗舰机，体验超棒！";
        post2.time = "2小时前";
        post2.comments.add(new Comment("张三", "多少钱入的？"));
        postList.add(post2);
    }
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentDiscoverBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null) {
            ActionBar supportActionBar = activity.getSupportActionBar();
            if (supportActionBar != null) {
                View customView = supportActionBar.getCustomView();
                TextView textViewTitle = customView.findViewById(R.id.textView_title);
                ImageView imageViewLogo = customView.findViewById(R.id.imageView_logo);
                textViewTitle.setText("发现");
            }
        }
        ListView listView = root.findViewById(R.id.notificationelv);
        addStatic();
        adapter = new DiscoverAdapter(requireContext(), postList);
        listView.setAdapter(adapter);
        ImageButton fabPost = root.findViewById(R.id.fab_post);
        fabPost.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), PostActivity.class);
            startActivity(intent);
        });
        return root;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}